package com.wildwolf.project.wildwolf.ui.register;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.wildwolf.project.wildwolf.R;
import com.wildwolf.project.wildwolf.config.AppConfig;

public class IntroductionActivity extends Activity implements
		View.OnClickListener {
	/**
	 * 上一步
	 */
	private TextView prev;
	/**
	 * 下一步
	 */
	private TextView next;
	/**
	 * 
	 */
	private Intent intent;
	/**
	 * 昵称
	 */
	private EditText editTextNikeName;
	/**
	 * 性别
	 */
	private RadioGroup radioGroupMale;
	/**
	 * 
	 */
	private boolean isLegalNikeName = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_reg_intro);
		intent = getIntent();
		/**
		 * 初始化试图
		 */
		initViews();
		/**
		 * 初始化监听器
		 */
		initEventListener();
	}

	private void initViews() {
		prev = (TextView) findViewById(R.id.textView_Intro_Prev);
		next = (TextView) findViewById(R.id.textView_Intro_Next);
		editTextNikeName = (EditText) findViewById(R.id.editText_nikeName);
		radioGroupMale = (RadioGroup) findViewById(R.id.radioGroupSex);
	}

	private void initEventListener() {
		prev.setOnClickListener(this);
		next.setOnClickListener(this);
		editTextNikeName.addTextChangedListener(new NikeNameTextWatcher());
	}

	@Override
	public void onClick(View v) {
		if (v == prev) {
			intent.setClass(this, AccountActivity.class);
			finish();
		} else if (v == next) {
			int selectedSex = radioGroupMale.getCheckedRadioButtonId();
			if (selectedSex == R.id.radio_female) {
				intent.putExtra(
						AppConfig.REGISTERKEY.ENCOUNTER_REGISTER_GENDER,String.valueOf(AppConfig.GENDER.GENDER_FEMALE));
			} else {
				intent.putExtra(
						AppConfig.REGISTERKEY.ENCOUNTER_REGISTER_GENDER,String.valueOf(AppConfig.GENDER.GENDER_MALE));
			}
			intent.putExtra(AppConfig.REGISTERKEY.ENCOUNTER_REGISTER_USERNAME,editTextNikeName.getText().toString());
			intent.setClass(this, BirthdayActivity.class);
		}
		startActivity(intent);

	}

	/**
	 * 
	 * @param accountName
	 */
	private void verifyNikeName(String nikeName) {

		isLegalNikeName = !TextUtils.isEmpty(nikeName);

	}

	private class NikeNameTextWatcher implements TextWatcher {

		@Override
		public void afterTextChanged(Editable editable) {
			verifyNikeName(editable.toString());
			next.setEnabled(isLegalNikeName);
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {

		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {

		}
	}

}
